import 'package:flutter_wan_android/core/http/network_exception.dart';
import 'package:flutter_wan_android/data/banner/banner_entity.dart';
import 'package:flutter_wan_android/data/banner/banner_local_api.dart';
import 'package:flutter_wan_android/data/banner/banner_network_api.dart';
import 'package:flutter_wan_android/data/logic_result.dart';

class BannerRepository {
  final BannerNetworkApi _networkApi;
  final BannerLocalApi _localApi;

  BannerRepository(this._networkApi, this._localApi);

  Stream<LogicResult<List<BannerEntity>>> requestList() async* {
    var list = await _localApi.getList();
    if (list.isNotEmpty) {
      yield LogicResult.ok(list);
    }

    try {
      list = await _networkApi.requestList();
      _localApi.saveList(list);
      yield LogicResult.ok(list);
    } on NetworkException catch (e) {
      yield LogicResult.failed(e.code, e.message);
    }
  }

}
